 // pages/cart/cart.js
const app = getApp()
const Host = require('../../config').Host
Page({

    /**
     * 页面的初始数据
     */
    data: {
        successShow: false,
        goodsList: [],
        all_choose: false, //是否全部选中
        all_delete_choose: false, //开启删除后是否全部选中
        total_price: '0.00', //总价
        count: 0, //选中几件商品
        deleteCount: 0, //开启删除后选中商品的数量
        deleteShow: false, //是否开启删除并显现加减框
        numIndex: -1,
    },
    getTouchStart: function(e) {
        var that = this;
        var index = e.currentTarget.id;
        var goodsList = that.data.goodsList;
        var windowWidth = wx.getSystemInfoSync().windowWidth;
        goodsList[index].x1 = e.changedTouches[0].pageX;
        goodsList[index].y1 = e.changedTouches[0].pageY;
        that.setData({
            goodsList: goodsList,
            windowWidth: windowWidth,
        })
    },
    getTouchMove: function(e) {
        var that = this;
        // 屏幕宽度      
        var index = e.currentTarget.id;
        var goodsList = that.data.goodsList;
        var windowWidth = that.data.windowWidth;
        goodsList[index].x2 = e.changedTouches[0].pageX;
        goodsList[index].y2 = e.changedTouches[0].pageY;
        that.setData({
            goodsList: goodsList,
        })
        // console.log(goodsList[index].x1, goodsList[index].y1)
        // console.log(goodsList[index].x2, goodsList[index].y2)
        if (goodsList[index].x1 - goodsList[index].x2 > 0 && goodsList[index].x1 - goodsList[index].x2 > Math.abs(goodsList[index].y1 - goodsList[index].y2) && goodsList[index].move != 100) {
            var move = (goodsList[index].x1 - goodsList[index].x2) / windowWidth * 750;
            if (move > 100) {
                move = 100;
            }
            goodsList[index].move = move;
        } else if (goodsList[index].x2 - goodsList[index].x1 > 0 && goodsList[index].x2 - goodsList[index].x1 > Math.abs(goodsList[index].y1 - goodsList[index].y2) && goodsList[index].move != 0) {
            var move2 = 100 - (goodsList[index].x2 - goodsList[index].x1) / windowWidth * 750;
            if (move2 < 0) {
                move2 = 0;
            }
            goodsList[index].move = move2;
        }
        that.setData({
            goodsList: goodsList,
        })
    },
    getTouchEnd: function(e) {
        var that = this;
        var index = e.currentTarget.id;
        var goodsList = that.data.goodsList;
        var windowWidth = that.data.windowWidth; // 屏幕宽度    
        var move = goodsList[index].move;
        if (goodsList[index].x1 > goodsList[index].x2) {
            if (move <= 30) {
                move = 0;
            } else {
                move = 100;
            }
        } else {
            if (move < 100) {
                move = 0;
            }
        }
        goodsList[index].move = move;
        that.setData({
            goodsList: goodsList,
        })
    },
    //选中
    chooseCirle: function(e) {
        var index = e.currentTarget.dataset.id;
        var that = this;
        var goodsList = that.data.goodsList;
        if (goodsList[index].isChoose) {
            goodsList[index].isChoose = false;
        } else {
            goodsList[index].isChoose = true;
        }
        that.setData({
            goodsList: goodsList,
        })
        that.getAllChoose();
        that.cac();
    },
    //删除开启时选中
    chooseCirle2: function(e) {
        var index = e.currentTarget.dataset.id;
        var that = this;
        var goodsList = that.data.goodsList;
        if (goodsList[index].isDeleteChoose) {
            goodsList[index].isDeleteChoose = false;
        } else {
            goodsList[index].isDeleteChoose = true;
        }
        that.setData({
            goodsList: goodsList,
        })
        that.getAllChoose2();
    },
    //全部选中//全部取消
    allChoose: function() {
        var that = this;
        var goodsList = that.data.goodsList;
        var all_choose = that.data.all_choose;
        if (all_choose == false) {
            all_choose = true;
            goodsList.forEach(function(item) {
                item.isChoose = true;
            })
            that.setData({
                count: goodsList.length,
            })

        } else {
            all_choose = false;
            goodsList.forEach(function(item) {
                item.isChoose = false;
            })
            that.setData({
                count: 0,
            })
        }
        that.setData({
            all_choose: all_choose,
            goodsList: goodsList,
        })
        that.cac();
    },
    //删除开启时全部选中//全部取消
    allChoose2: function() {
        var that = this;
        var goodsList = that.data.goodsList;
        var all_delete_choose = that.data.all_delete_choose;
        if (all_delete_choose == false) {
            all_delete_choose = true;
            goodsList.forEach(function(item) {
                item.isDeleteChoose = true;
            })
            that.setData({
                deleteCount: goodsList.length,
            })

        } else {
            all_delete_choose = false;
            goodsList.forEach(function(item) {
                item.isDeleteChoose = false;
            })
            that.setData({
                deleteCount: 0,
            })
        }
        that.setData({
            all_delete_choose: all_delete_choose,
            goodsList: goodsList,
        })
    },
    //判断事件触发下是否满足全选
    getAllChoose: function() {
        var arr = [];
        var that = this;
        var goodsList = that.data.goodsList;
        goodsList.forEach((item) => {
            if (item.isChoose) {
                arr.push(item.isChoose);
            }
        })
        if (arr.length == goodsList.length) {
            that.setData({
                all_choose: true,
            })
        } else {
            that.setData({
                all_choose: false,
            })
        }
        that.setData({
            goodsList: goodsList,
            count: arr.length,
        })
    },
    //删除开启时判断事件触发下是否满足全选
    getAllChoose2: function() {
        var arr = [];
        var that = this;
        var goodsList = that.data.goodsList;
        goodsList.forEach(item => {
            if (item.isDeleteChoose) {
                arr.push(item.isDeleteChoose);
            }
        })
        if (arr.length == goodsList.length) {
            that.setData({
                all_delete_choose: true,
            })
        } else {
            that.setData({
                all_delete_choose: false,
            })
        }
        that.setData({
            goodsList: goodsList,
            deleteCount: arr.length,
        })
    },
    //获取加减框下标
    getNumIndex: function(e) {
        var index = e.currentTarget.dataset.id;
        this.setData({
            numIndex: index,
        })
    },
    //获取input框值
    getCount: function(e) {
        var that = this;
        var goodsList = that.data.goodsList;
        var numIndex = that.data.numIndex;
        goodsList[numIndex].num = e.detail.value;
        that.setData({
            goodsList: goodsList,
        })
    },
    //减
    del: function() {
        var that = this;
        var numIndex = that.data.numIndex;
        var goodsList = that.data.goodsList;
        goodsList[numIndex].num--;
        if (goodsList[numIndex].num < 1) {
            goodsList[numIndex].num = 1;
            wx.showToast({
                title: '至少选择一个商品',
                icon: 'none',
            })
        }
        that.setData({
            goodsList: goodsList,
        })
    },
    //加
    add: function() {
        var that = this;
        var numIndex = that.data.numIndex;
        var goodsList = that.data.goodsList;
        var buynum = goodsList[numIndex].buynum;
        if (goodsList[numIndex].num >= buynum) {
          goodsList[numIndex].num = buynum;
        } else {
          goodsList[numIndex].num++;
        }
        that.setData({
            goodsList: goodsList,
        })
    },
    //计算总价格
    cac: function() {
        var that = this;
        var goodsList = that.data.goodsList;
        var priceList = [];
        goodsList.forEach(item => {
            if (item.isChoose) {
                var price1 = parseFloat(item.price) * item.num;
                priceList.push(price1)
            }
        })
        console.log(priceList)
        var total_price = 0;
        if (priceList.length > 0) {
            priceList.forEach(item => {
                total_price += item;
            })
        }
        that.setData({
            total_price: total_price.toFixed(2),
        })
    },
    //开启删除并显现加减框
    openDeleteShow: function() {
        this.setData({
            deleteShow: true,
        })
    },
    //关闭
    closeDeleteShow: function() {
        var that = this;
        var goodsList = that.data.goodsList;
        goodsList.forEach(item => {
            item.isDeleteChoose = false;
        })
        that.setData({
            goodsList: goodsList,
            all_delete_choose: false,
            deleteCount: 0,
            deleteShow: false,
        })
        that.cac();
    },
    //删除
    delete1: function(e) {
        var that = this;
        var index = e.currentTarget.dataset.id;
        var goodsList = that.data.goodsList;
        var openid = wx.getStorageSync('openid');
        wx.showModal({
            title: '',
            content: '确定要删除吗?',
            showCancel: true,
            success: function(res) {
                if (res.cancel) {
                    goodsList[index].move =0;
                    that.setData({
                        goodsList: goodsList,
                    })
                } else {
                  var id = goodsList[index].record_id;
                  //console.log(id) //删除商品的id
                  wx.request({
                    url: Host + 'Default/delgoods',
                    data: { id, openid },
                    method: 'post',
                    header: { 'content-type': 'application/x-www-form-urlencoded' },
                    success: function (res) {
                      if (res.data.code==200) {
                        goodsList.splice(index, 1);
                        that.getAllChoose();
                        that.getAllChoose2();
                        that.cac();
                      } else {
                        wx.showToast({
                          title: res.data.errmsg,
                          icon: 'none',
                        })
                      }
                    },
                  })
                }
            }
        })
    },
    //多个删除
    delete2: function() {
        var that = this;
        var goodsList = that.data.goodsList;
        var openid = wx.getStorageSync('openid');
        if (that.data.deleteCount > 0) {
            wx.showModal({
                title: '',
                content: '确定要删除选中的商品吗?',
                showCancel: true,
                success: function(res) {
                    if (res.cancel) {} else {
                        var arr = [];
                        var allId = []; //此次删除操作要删除的数据的id
                        goodsList.forEach((item, idx) => {
                            if (item.isDeleteChoose) {
                              allId.push(item.record_id);
                              arr.push(idx)
                            }
                        })
                        //删除之后恢复最初界面
                        that.setData({
                            deleteShow: false,
                        })
                        //console.log(allId)
                      wx.request({
                        url: Host + 'Default/delgoods',
                        data: { "id": allId, openid },
                        method: 'post',
                        header: { 'content-type': 'application/x-www-form-urlencoded' },
                        success: function (res) {
                          if (res.data.code == 200) {
                            for (var n = 0, k = 0; n < arr.length; n++ , k++) {
                              goodsList.splice(arr[n] - k, 1);
                            }
                            that.getAllChoose();
                            that.getAllChoose2();
                            that.cac();
                          } else {
                            wx.showToast({
                              title: res.data.errmsg,
                              icon: 'none',
                            })
                          }
                        },
                      })
                    }
                }
            })
        }
    },
    //结算
    banlace: function() {
        var that = this;
        var goodsList = that.data.goodsList;
        var arr = [];
        goodsList.forEach(item => {
            if (item.isChoose) {
                var obj = {};
                obj.goods_id = item.id;
                obj.record_id = item.record_id;
                obj.standard_id = item.standard_id;
                obj.goods_count = item.num;
                arr.push(obj);
            }
        })
        //console.log(arr)
        wx.setStorageSync('order', arr)
        if (arr.length>0){
            wx.navigateTo({
                url: '/pages/order/order',
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
       
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this;
        var openid = wx.getStorageSync('openid');
        wx.request({
            url: Host + 'Default/cart',
            data: { openid },
            success: function (res) {
                that.setData({
                    goodsList: res.data.list,
                    all_choose: res.data.all_choose,
                    total_price: '0.00',
                    count: 0,
                    all_delete_choose: false,
                    deleteCount:0,
                });
                if (that.data.goodsList) {
                    that.setData({
                        successShow: true,
                    })
                }
            },
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        wx.stopPullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})